package com.ja.openchat.ui.chat.ext;

import static android.app.Activity.RESULT_OK;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.view.View;

import com.ja.openchat.BuildConfig;
import com.ja.openchat.R;
import com.zy.openchat.core.model.message.ChatInfo;
import com.zy.openchat.core.model.message.LocationData;
import com.ja.openchat.ui.chat.LocationActivity;
import com.ja.openchat.ui.chat.annotation.ExtContextMenuItem;
import com.ja.openchat.ui.chat.ext.core.ConversationExt;

import pub.devrel.easypermissions.EasyPermissions;


public class LocationExt extends ConversationExt {

    /**
     * @param containerView 扩展view的container
     * @param conversation
     */
    @ExtContextMenuItem
    public void pickLocation(View containerView, ChatInfo conversation) {
        String[] permissions = new String[]{
            Manifest.permission.ACCESS_FINE_LOCATION,
            Manifest.permission.ACCESS_COARSE_LOCATION
        };
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (!EasyPermissions.hasPermissions(activity, permissions)) {
                EasyPermissions.requestPermissions(activity,
                        String.format(activity.getResources().getString(R.string.permission_basic_tip), activity.getResources().getString(R.string.app_name)),
                        100, permissions);
                return;
            }
        }

        Intent intent = new Intent(activity, LocationActivity.class);
        startActivityForResult(intent, 100);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            LocationData locationData = (LocationData) data.getSerializableExtra("location");
            messageViewModel.sendLocationMessage(conversation, locationData);
        }
    }

    @Override
    public boolean filter(ChatInfo conversation) {
        return BuildConfig.Is_ShowLocal||conversation.isGroupSendChat();
    }


    @Override
    public int priority() {
        return 100;
    }

    @Override
    public int iconResId() {
        return R.drawable.icon_location;
//        return R.mipmap.menu_location;
    }

    @Override
    public String title(Context context) {
        return context.getString(R.string.location_text);
    }

    @Override
    public String contextMenuTitle(Context context, String tag) {
        return title(context);
    }
}
